package f.h.dragon.reflection.invoker;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * MethodInvoker: the invoker of method 
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/04/24
 * @since 1.0
 */
public class MethodInvoker implements Invoker {

	// local variables
	/**
	 * the type of method
	 */
	private Class<?> type;
	/**
	 * method
	 */
	private Method method;

	// Constructor
	/**
	 * constructor MethodInvoker with method.
	 * 
	 * @param method
	 */
	public MethodInvoker(Method method) {
		this.method = method;

		if (method.getParameterTypes().length == 1) {
			type = method.getParameterTypes()[0];
		} else {
			type = method.getReturnType();
		}
	}

	/**
	 * invoke: see {@link java.lang.reflect.Method#invoke(Object, Object...)}
	 * 
	 * @param target
	 *            the object the underlying method is invoked from
	 * @param args
	 *            the arguments used for the method call
	 * @return the result of dispatching the method represented by this object
	 *         on <code>target</code> with parameters <code>args</code>
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public Object invoke(Object target, Object[] args)
			throws IllegalAccessException, InvocationTargetException {
		return method.invoke(target, args);
	}

	/**
	 * the type of method
	 * 
	 * @return
	 * 
	 * @see f.h.dragon.reflection.invoker.Invoker#getType()
	 */
	public Class<?> getType() {
		return type;
	}
}
